package com.example.fszq.volleydemo1.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.fszq.volleydemo1.utils.CatagoryList;
import com.example.fszq.volleydemo1.R;

import java.util.List;

/**
 * Created by fszq on 2015/12/16.
 */
public class CataogryAdapter  extends ArrayAdapter<CatagoryList> {
    private int resourceId;
    //private RequestQueue quence ;
    //private Requesttoserver Rserver = new Requesttoserver();
    //ImageLoader imageLoader = ImageLoader.getInstance();

    public CataogryAdapter(Context context, int CatViewResourceId, List<CatagoryList> objects) {
        super(context, CatViewResourceId, objects);
        resourceId = CatViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("cataogrykey:", position + "");

        CatagoryList cList = getItem(position);
        View view;
        ViewHolder viewHolder;
        int isTitle = 0;
        isTitle = Integer.parseInt(cList.getIstitle());

        if (convertView == null) {
            if(isTitle == 0) {
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                viewHolder = new ViewHolder();
                viewHolder.cid = (TextView) view.findViewById(R.id.cid);
                //viewHolder.clogo = (ImageView)view.findViewById(R.id.clogo);

                //if (imageLoader == null)
                //    imageLoader = ImageLoader.getInstance();

                //viewHolder.clogo =  (NetworkImageView)view.findViewById(R.id.clogo);
                viewHolder.clogo = (ImageView) view.findViewById(R.id.clogo);
                viewHolder.cname = (TextView) view.findViewById(R.id.cname);
                view.setTag(viewHolder); // 将ViewHolder存储在View中
            } else {
                view = LayoutInflater.from(getContext()).inflate(R.layout.catagory_title, null);
                viewHolder = new ViewHolder();
                viewHolder.titlename = (TextView) view.findViewById(R.id.titlename);
                view.setTag(viewHolder);
            }
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder

        }

        if(isTitle == 0) {
            viewHolder.cid.setText(cList.getCid());
            //viewHolder.clogo.setImageUrl(cList.getClogo(), imageLoader);
            //imageLoader.displayImage(cList.getClogo(), viewHolder.clogo);

            Glide.with(getContext())
                    .load(cList.getClogo())
                    .into(viewHolder.clogo);
            viewHolder.cname.setText(cList.getCname());
            return view;
        } else {
            viewHolder.titlename.setText(cList.getCname());
            return view;

        }
    }


    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if(getItemViewType(position)==0) {
            return true;
        } else {
            return false;
        }
        //return getItemViewType(position) != TYPE_CATEGORY_ITEM;
    }

    @Override
    public int getItemViewType(int position) {
        // 异常情况处理
        //if (null == mListData || position <  0|| position > getCount()) {
        //    return null;
        //}

        CatagoryList cList = getItem(position);
        int isTitle = 0;
        isTitle = Integer.parseInt(cList.getIstitle());
        return isTitle;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    class ViewHolder {
        TextView cid,cname,cdesc,titlename;
        ImageView clogo;
    }
}
